package com.ml.doctor.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;

import com.ml.doctor.CustomApplication;
import com.ml.doctor.call2.NimAccountHelper;
import com.ml.doctor.call2.QianZui;

public class SplashActivity extends BaseActivity {
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideTopTitle();

        boolean isAllGranted = checkPermissionAllGranted(
                new String[] {
                        Manifest.permission.CAMERA,
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.RECORD_AUDIO
                }
        );
        // 如果这3个权限全都拥有, 则直接执行备份代码
        if (isAllGranted) {
            if (CustomApplication.getInstance().userId != 0 && CustomApplication.getInstance().userId != -1){
                startActivity(new Intent(mContext, MainActivity.class));
                NimAccountHelper.getInstance().login(QianZui.qianZuiDoc + CustomApplication.getInstance().userId, "123456",null);
            } else {
                startActivity(new Intent(mContext, LoginActivity.class));
            }
            finish();
            return;
        }

        /**
         * 第 2 步: 请求权限
         */
        // 一次请求多个权限, 如果其他有权限是已经授予的将会自动忽略掉
        ActivityCompat.requestPermissions(
                this,
                new String[] {
                        Manifest.permission.CAMERA,
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.RECORD_AUDIO
                },
                0
        );
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void checkPermission(){
        if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
        } else {
        }
    }

    private boolean checkPermissionAllGranted(String[] permissions) {
        for (String permission : permissions) {
            if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                // 只要有一个权限没有被授予, 则直接返回 false
                return false;
            }
        }
        return true;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (CustomApplication.getInstance().userId != 0 && CustomApplication.getInstance().userId != -1){
            startActivity(new Intent(mContext, MainActivity.class));
            NimAccountHelper.getInstance().login(QianZui.qianZuiDoc + CustomApplication.getInstance().userId, "123456",null);
        } else {
            startActivity(new Intent(mContext, LoginActivity.class));
        }
        finish();
        switch (requestCode) {
            case 0:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                } else {
                    Toast.makeText(mContext, "缺少权限，请打开权限", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
